using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.DataTier.Source;

namespace OR.Lazarus.Library.DataTier.Source.Lazarus
{
	/// <summary>
	/// Defines an index on a field.  This is used for PK determination, as well as database performance.
	/// </summary>
   public class IndexDetail : EntityBase
   {
		protected System.String mName;
		protected string mAlias;
		protected string mAccessor;
		protected string mObject;
		protected string mType;
		protected string mOwnerField;
		protected string mFilegroup;
		protected string mConstraintName;
		protected System.Boolean mCascadeDelete;
		protected string mDescription;

		public IndexDetail()
		{
	
		}
	
		/// <summary>
		/// Specifies a unique name for the entity.
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// The Alias defines the index's name when used in a SQL context.  It defaults to the lowercase representation of the index Name.
		/// </summary>
		public string Alias
		{
			get
			{
				return ( mAlias );
			}
			set
			{
				mAlias = value;
			}
		}

		/// <summary>
		/// The Accessor is the name of the index appropriate to a logical context.  This would be used, for example, on a C# Object Model.
		/// </summary>
		public string Accessor
		{
			get
			{
				return ( mAccessor );
			}
			set
			{
				mAccessor = value;
			}
		}

		/// <summary>
		/// This specifies the name of the object that the index is a reference to.  This is typically used when the Index Type is Foreign.
		/// </summary>
		public string Object
		{
			get
			{
				return ( mObject );
			}
			set
			{
				mObject = value;
			}
		}

		/// <summary>
		/// Defines the Type of Index.  Valid Index Types are: * Clustered: The object rows are stored in this index's order.  This is typically used in a relational database scenario. * Primary: The index uniquely identifies a row. * Foreign: The index is a reference to a row in another object.
		/// </summary>
		public string Type
		{
			get
			{
				return ( mType );
			}
			set
			{
				mType = value;
			}
		}

		/// <summary>
		/// Unknown
		/// </summary>
		public string OwnerField
		{
			get
			{
				return ( mOwnerField );
			}
			set
			{
				mOwnerField = value;
			}
		}

		/// <summary>
		/// Specifies the physical storage name identifier for the index information.  This is typically used in a relational database scenario.
		/// </summary>
		public string Filegroup
		{
			get
			{
				return ( mFilegroup );
			}
			set
			{
				mFilegroup = value;
			}
		}

		/// <summary>
		/// Specifies the name of the index constraint.  This is typically used with Foreign indexes in a relational database scenario.
		/// </summary>
		public string ConstraintName
		{
			get
			{
				return ( mConstraintName );
			}
			set
			{
				mConstraintName = value;
			}
		}

		/// <summary>
		/// Specifies whether or not to cascade deletes of related rows.  This is typically used with Foreign indexes in a relational database scenario.
		/// </summary>
		public System.Boolean CascadeDelete
		{
			get
			{
				return ( mCascadeDelete );
			}
			set
			{
				mCascadeDelete = value;
			}
		}

		/// <summary>
		/// Provides a long-text description for the entity.
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mAlias = DataUtility.GetString( pSource, "alias" );
			mAccessor = DataUtility.GetString( pSource, "accessor" );
			mObject = DataUtility.GetString( pSource, "object" );
			mType = DataUtility.GetString( pSource, "type" );
			mOwnerField = DataUtility.GetString( pSource, "ownerfield" );
			mFilegroup = DataUtility.GetString( pSource, "filegroup" );
			mConstraintName = DataUtility.GetString( pSource, "constraintname" );
			mCascadeDelete = DataUtility.GetBool( pSource, "cascade-delete", false );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lz:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lz", "index", "http://schemas.icitadel.com/Lazarus/Source.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mAlias ) )
			{
				vNewElement.SetAttribute( "alias", mAlias );
			}
					
			if ( !String.IsNullOrEmpty( mAccessor ) )
			{
				vNewElement.SetAttribute( "accessor", mAccessor );
			}
					
			if ( !String.IsNullOrEmpty( mObject ) )
			{
				vNewElement.SetAttribute( "object", mObject );
			}
					
			if ( !String.IsNullOrEmpty( mType ) )
			{
				vNewElement.SetAttribute( "type", mType );
			}
					
			if ( !String.IsNullOrEmpty( mOwnerField ) )
			{
				vNewElement.SetAttribute( "ownerfield", mOwnerField );
			}
					
			if ( !String.IsNullOrEmpty( mFilegroup ) )
			{
				vNewElement.SetAttribute( "filegroup", mFilegroup );
			}
					
			if ( !String.IsNullOrEmpty( mConstraintName ) )
			{
				vNewElement.SetAttribute( "constraintname", mConstraintName );
			}
					
			vNewElement.SetAttribute( "cascade-delete", mCascadeDelete.ToString() );
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lz", "description", "http://schemas.icitadel.com/Lazarus/Source.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.IndexDetail v_oReturn = new Derived.IndexDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Alias = mAlias;
			v_oReturn.Accessor = mAccessor;
			v_oReturn.Object = mObject;
			v_oReturn.Type = mType;
			v_oReturn.OwnerField = mOwnerField;
			v_oReturn.Filegroup = mFilegroup;
			v_oReturn.ConstraintName = mConstraintName;
			v_oReturn.CascadeDelete = mCascadeDelete;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections
			
			return ( v_oReturn );
		}

		public virtual Derived.IndexDetail CloneTyped()
		{
			return ( ( Derived.IndexDetail )this.Clone() );
		}
   }
}
   